package com.ws.lv.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.lv.bean.Acl_user;
import com.ws.lv.bean.Route;
import com.ws.lv.bean.Supplier;
import com.ws.lv.service.Acl_userService;
import com.ws.lv.service.RouteService;
import com.ws.lv.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    SupplierService supplierService;
    @Autowired
    RouteService routeService;
    @Autowired
    Acl_userService acl_userService;

    //去供应商信息页面
    @RequestMapping("/selectSupplier")
    public String selectSupplier() {
        return "adminpage/supplier/list";
    }

    //去添加供应商页面
    @RequestMapping("/toAddSupplier")
    public String toAddSupplier() {
        return "adminpage/supplier/add";
    }

    //去修改供应商页面
    @RequestMapping("/toUpdateSupplier")
    public String toUpdateSupplier(int supplier_id, Model model) {
        Supplier supplier = supplierService.selectSupplierById(supplier_id);
        model.addAttribute("supplier", supplier);
        return "adminpage/supplier/update";
    }

    //多条件查询供应商信息页面
    @RequestMapping("/selectSuppliers")
    public String selectSuppliers(Model model, String supplier_name,Integer currentPage) {
        PageHelper pageHelper = new PageHelper();//分页帮助类
        if (currentPage == null){
            pageHelper.startPage(1, 5);
        }else {
            pageHelper.startPage(currentPage, 5);
        }
        List<Supplier> suppliers = supplierService.selectSuppliers(supplier_name);
        PageInfo pageInfo = new PageInfo(suppliers);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("supplier_name", supplier_name);
        return "adminpage/supplier/list";
    }
    
    //添加供应商信息
    @RequestMapping("/insertSupplier")
    public String insertSupplier(Supplier supplier) {
        int i = supplierService.insertSupplier(supplier);
        return "redirect:/supplier/selectSuppliers";
    }

    //修改供应商信息
    @RequestMapping("/updateSupplier")
    public String updateSupplier(Supplier supplier) {
        int i = supplierService.updateSupplier(supplier);
        return "redirect:/supplier/selectSuppliers";
    }

    //删除供应商信息
    @RequestMapping("/deleteSupplierById")
    public String deleteSupplierById(int supplier_id) {
        List<Route> routes = routeService.selectRoutesBySupplierId(supplier_id);
        List<Acl_user> aclUsers = acl_userService.selectAcl_usersBySupplierId(supplier_id);
        boolean flag = true;
        if (routes.size() > 0 || aclUsers.size() > 0){
            System.out.println(routes);
            System.out.println(aclUsers);
            flag = false;
        }
        if (flag){
            int i = supplierService.deleteSupplierById(supplier_id);
        }
        return "redirect:/supplier/selectSuppliers";


    }

    //批量删除供应商信息
    @RequestMapping("/deleteSuppliers")
    public String deleteSuppliers(String supplier_ids) {
        String[] split = supplier_ids.split(",");
        ArrayList<Integer> list = new ArrayList<>();
        for (String s : split) {
            list.add(Integer.parseInt(s));
        }
        int i = supplierService.deleteSuppliers(list);
        return "redirect:/supplier/selectSuppliers";
    }




}
